package com.huangliusong.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.bouncycastle.jcajce.provider.asymmetric.dsa.DSASigner.detDSA;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonView;
import com.huangliusong.dto.User;
import com.huangliusong.dto.UserQueryCondition;
import com.huangliusong.exception.UserNotExistException;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/user")
public class UserController {
	@GetMapping("/my")
	public Object getCurrentUser() {
		return SecurityContextHolder.getContext();
	}
	@DeleteMapping("/{id:\\d+}")
	public void delete(@PathVariable String id) {
		System.err.println(id);
	}

	@PostMapping()
	public User createUser(@Valid @RequestBody User user) {
		System.err.println(user.getBirthday());
		System.err.println(user.getId());
		System.err.println(user.getUsername());
		System.err.println(user.getPassword());

		user.setId("1");
		return user;
	}

	@PutMapping("/{id:\\d+}")
	public User updateUser(@Valid @RequestBody User user, BindingResult errors) {
		if (errors.hasErrors()) {
			errors.getAllErrors().stream().forEach(error -> {
				FieldError fieldError = (FieldError) error;
				String message = fieldError.getField() + "\t" + error.getDefaultMessage();
				System.err.println(message);
			});
		}
		System.err.println(user.getBirthday());
		System.err.println(user.getId());
		System.err.println(user.getUsername());
		System.err.println(user.getPassword());

		user.setId("1");
		return user;
	}

	// @RequestMapping(value = "/user", method = RequestMethod.GET)
	@GetMapping()
	@JsonView(User.UserSimpleView.class)
	@ApiOperation(value="这个是方法的描述")
	public List<User> query(UserQueryCondition userQueryCondition,
			@PageableDefault(page = 2, size = 17, sort = "username,asc") Pageable pageable) {
		System.err.println(ReflectionToStringBuilder.toString(userQueryCondition, ToStringStyle.MULTI_LINE_STYLE));
		System.out.println(pageable.getPageSize());
		System.out.println(pageable.getPageNumber());
		System.out.println(pageable.getSort());
		List<User> users = new ArrayList<>();
		users.add(new User());
		users.add(new User());
		users.add(new User());
		return users;
	}

	// 限制输入只能数字不能字符串
	// @RequestMapping(value="/user/{id:\\d+}",method=RequestMethod.GET)
	@GetMapping("/{id:\\d+}")
	@JsonView(User.UserDetaliView.class)
	public User getInfo(@ApiParam("用户id")@PathVariable(name = "id") String idxxx) {
		// throw new UserNotExistException(idxxx);
		System.err.println("进入getInfo服务");
		User user = new User();
		user.setPassword("111111");
		user.setUsername("huangliusong");
		return user;

	}

}
